package com.example.demo.controller;

import com.example.demo.entity.Goods;
import com.example.demo.service.IGoodsService;

import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表
     */
    @PassToken
    @GetMapping("/list")
    public ServiceResponse list(Goods goods, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "3")int pageSize)
    {
        PageResult pageResult = goodsService.selectGoodsList(goods,pageNum,pageSize);

        return ServiceResponse.createBySuccess(200,pageResult,"获取成功");
    }

    /**
     * 导出商品列表
     */



    /**
     * 获取商品详细信息
     */

    @GetMapping(value = "/{gid}")
    public ServiceResponse getInfo(@PathVariable("gid") Long gid)
    {
        return ServiceResponse.createBySuccess(200,goodsService.selectGoodsByGid(gid),"获取成功");
    }

    /**
     * 新增商品
     */

    @PostMapping
    public ServiceResponse add(@RequestBody Goods goods)
    {
        int i = goodsService.insertGoods(goods);
        if (i>0){
            return ServiceResponse.createBySuccess(200,",","添加成功");
        }else {
            return ServiceResponse.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改商品
     */

    @PutMapping
    public ServiceResponse edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if (i>0){
            return ServiceResponse.createBySuccess(200,",","更新成功");
        }else {
            return ServiceResponse.createByFail(500,"更新失败");
        }
    }

    /**
     * 删除商品
     */

	@DeleteMapping("/{gids}")
    public ServiceResponse remove(@PathVariable Long[] gids)
    {
       int i = goodsService.deleteGoodsByGids(gids);
       if (i>0){
           return ServiceResponse.createBySuccess(200,",","删除成功");
       }else {
           return ServiceResponse.createByFail(500,"删除失败");
       }
    }
}
